package com.haulmont.asu.web.ui.work;

/**
 * Author: Sergey42
 * Date: 14.11.13 10:43
 */
/*
 * Copyright (c) 2012 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */

import com.haulmont.asu.entity.Work;
import com.haulmont.charts.core.entity.GanttChartTask;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.TimeProvider;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * @author subbotin
 * @version $Id$
 */
public class GanttChartLoader {

    private CollectionDatasource ganttDatasource;

    public static GanttChartLoader createLoader(CollectionDatasource source) {
        return new GanttChartLoader(source);
    }


    protected GanttChartLoader(CollectionDatasource source) {
        this.ganttDatasource = source;
    }

    public void loadFromDatasource(CollectionDatasource.Ordered<Work, UUID> taskDatasource) {
        ganttDatasource.clear();
        for (UUID taskID = taskDatasource.firstItemId(); taskID != null; taskID = taskDatasource.nextItemId(taskID)) {
            Work task = taskDatasource.getItem(taskID);
            ganttDatasource.includeItem(buildGanttItem(task));
        }
        //TODO: Hack to show gantt chart without tasks
        if (taskDatasource.size() == 0)
            ganttDatasource.includeItem(buildEmptyItem());
    }

    private Entity buildGanttItem(Work work) {
        GanttChartTask item = new GanttChartTask();

        item.setName(StringUtils.isEmpty(work.getName()) ? "" : work.getName());
        item.setOpen(true);
        item.setCaptionType("Resource");
       /* item.setRelatedEntity(task);
        item.setCompletePercent(task.getPercentCompletion());
        item.setInitiatorName(getInitiator(task));
        item.setResourceName(getResource(task));
        item.setTooltip(getTooltip(task));
        item.setStartTs(getStartDate(task));
        item.setEndTs(getFinishDate(task));
        item.setStyleClass(getStyleClass(task));*/

        return item;
    }

    private Entity buildEmptyItem() {
        GanttChartTask item = new GanttChartTask();
        item.setOpen(false);
        return item;
    }


   /* protected String getStyleClass(Q task) {
        String state = task.getState();
        if (StringUtils.isEmpty(state) || TaskState.New.getId().equals(state) || TaskState.DelayedStart.getId().equals(state)) {
            return "thesis-gantt-task-new";
        } else if (TaskState.Finished.getId().equals(state) || TaskState.Canceled.getId().equals(state))
            return "thesis-gantt-task-completed";
        else {
            Date dueDate = task.getFinishDateTimePlan();
            if (dueDate != null && dueDate.compareTo(TimeProvider.currentTimestamp()) <= 0) {
                return "thesis-gantt-task-overdue";
            }
            return "";
        }

    }*/

    protected Date getStartDate(Work task) {
        if (task.getFactStartDate() != null)
            return task.getFactStartDate();

        return TimeProvider.currentTimestamp();
    }

    protected Date getFinishDate(Work task) {
        return task.getFactFinishDate();
    }

}

